using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace MonoCash.ObjectModel
{
    class CurrencyConversion
    {
        public static void Add(Currency primary, Currency secondary, DateTime date, Decimal rate) 
        {
            DataRow row = Program.AppSettings.Data.CurrencyConversions.NewRow();
            row["CurrencyID1"] = primary.ID;
            row["CurrencyID2"] = secondary.ID;
            row["Date"] = date;
            row["Rate"] = rate;
            Program.AppSettings.Data.CurrencyConversions.Rows.Add(row);
        }
        public static Decimal GetRate(Currency primary, Currency secondary, DateTime date)
        {
            Decimal temp = 1;
            try
            {
                string filter = "Date <= '" + date.ToLongDateString() + "' and CurrencyID1=" + primary.ID.ToString() + " and CurrencyID2=" + secondary.ID.ToString();

                DateTime bestDate = (DateTime)Program.AppSettings.Data.CurrencyConversions.Compute("Max(Date)", filter);
                filter = filter.Replace("<=", "=");
                filter = filter.Replace(date.ToLongDateString(), bestDate.ToLongDateString());

                temp = (Decimal)Program.AppSettings.Data.Prices.Select(filter)[0]["Rate"];
            }
            catch { }
            return temp;
        }
    }
}
